﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Automation;
using System.Diagnostics;
using System.Windows;
using WinForms = System.Windows.Forms;

namespace GUI.Tabs
{
    class WindowSelector
    {
        Button Window_Selector;
        bool active;
        System.Timers.Timer timer;
        IAlerts alerts;

        public WindowSelector(Button windowSelector, IAlerts alerts)
        {
            this.alerts = alerts;

            Window_Selector = windowSelector;
            
            timer = new System.Timers.Timer(25);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTick);

            IsActive = false;
        }

        public bool IsActive
        {
            set
            {
                bool gnu = value;
                if (gnu)
                {
                    Window_Selector.Foreground = System.Windows.Media.Brushes.Red;
                    Window_Selector.Content = "Select Window\n(Drag and release)";
                    Mouse.OverrideCursor = System.Windows.Input.Cursors.Cross;
                    timer.Enabled = true;
                }
                else
                {
                    Window_Selector.Foreground = System.Windows.Media.Brushes.Black;
                    Window_Selector.Content = "Select Window\n(Click and hold)";
                    Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                    timer.Enabled = false;
                }
                active = gnu;
            }

            get
            {
                return active;
            }
        }

        private AutomationElement ae_temp;
        
        public delegate void ProcEvent(Process p);
        public event ProcEvent ProcessTargeted;

        public void OnTick(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (IsActive)
            {
                Window_Selector.Dispatcher.Invoke(
                    (Action)delegate()
                    {
                        if (Mouse.LeftButton == MouseButtonState.Released)
                        {
                            IsActive = false;

                            if (ae_temp == null)
                            {
                                alerts.Error("Unable to record events:\nbase element null");
                                return;
                            }
                            else
                            {
                                try { if (CheckIDs(ae_temp)) throw new IllegalTargetException(); }
                                catch (IllegalTargetException err) { alerts.Error(err.Message); return; }
                            }

                            //Makes sure this is the element the user wants to track
                            if (alerts.Confirm("Record events from this element?"))
                            {
                                int id = ae_temp.Current.ProcessId;
                                Process process = Process.GetProcessById(id);
                                ProcessTargeted(process);
                            }
                        }
                    }
                );
            }
            if (IsActive) //If still active...
            {
                //Gets position of the cursor
                Point p = new Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
                //Gets an automation element based on cursor position
                ae_temp = AutomationElement.FromPoint(p);
            }
        }

        private bool CheckIDs(AutomationElement ae)
        {
            foreach (int id in ae.GetRuntimeId())
            {
                if (id == Process.GetCurrentProcess().Id)
                    return true;
            }

            int ids = (int)ae.GetCurrentPropertyValue(AutomationElement.ProcessIdProperty);
            if (ids == Process.GetCurrentProcess().Id)
                return true;

            return false;
        }

    }
}
